package com.example.frame;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView iv_walk;
    private Button btn_walk;
    private AnimationDrawable animation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_walk = (ImageView) findViewById(R.id.iv_walk);
        btn_walk = (Button) findViewById(R.id.btn_play);
        btn_walk.setOnClickListener(this);
        animation = (AnimationDrawable) iv_walk.getBackground();
    }

    @Override
    public void onClick(View view) {
        if(!animation.isRunning()) {
            animation.start();
            btn_walk.setText("暂停");
        }else {
            animation.stop();
            btn_walk.setText("播放");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(animation.isRunning()) {
            animation.stop();
        }
        iv_walk.clearAnimation();
    }
}